package com.wenheng.comfig;

import com.wenheng.commer.DataSourceType;
import com.wenheng.dataSourceRouting.DynamicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DataSourceConfig {
    @Value("${mybatis.mapper-locations}")
    private String mapperLocations;
    @Value("${mybatis.config-location}")
    private String configLocation;

    @Primary
    @Bean(name = "master")
    @ConfigurationProperties(prefix = "master")
    public DataSource master() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "arsmanagesupport")
    @ConfigurationProperties(prefix = "arsmanagesupport")
    public DataSource arsmanagesupport() {
        return DataSourceBuilder.create().build();
    }

    /**
     * 动态配置数据库
     *
     * @author JohnDeng
     * @datatime 2019年4月1日上午10:00:01
     * @return
     */
    @Bean(name = "dynamicDataSource")
    public DynamicDataSource createDynamicDataSource() {
        // 实例化
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        // 设置数据源
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.put(DataSourceType.master, master());
        targetDataSources.put(DataSourceType.arsmanagesupport, arsmanagesupport());
        dynamicDataSource.setTargetDataSources(targetDataSources);
        // 设置默认数据源
        dynamicDataSource.setDefaultTargetDataSource(master());
        return dynamicDataSource;
    }

    /**
     * 配置mybaits
     *
     * @author JohnDeng
     * @datatime 2019年4月7日下午9:29:00
     * @return
     * @throws Exception
     */
    @Bean
    public SqlSessionFactory createSqlSessionFactoryBean() throws Exception {
        final SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        // 配置mapper的扫描，找到所有的mapper.xml映射文件
        Resource[] resources = new PathMatchingResourcePatternResolver().getResources(mapperLocations);
        // 配置mapper的扫描，找到所有的mapper.xml映射文件
        sqlSessionFactoryBean.setMapperLocations(resources);
        // 配置类型别名
        //sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
        // 加载全局的配置文件
        sqlSessionFactoryBean.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
        // 添加数据源
        sqlSessionFactoryBean.setDataSource(createDynamicDataSource());
        return sqlSessionFactoryBean.getObject();
    }





}
